package com.sanjiahantang.youjiarole.module.me.scheme.views.impl;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.sanjiahantang.uilibrary.adapter.listview.BaseAdapterHelper;
import com.sanjiahantang.uilibrary.adapter.listview.QuickAdapter;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.base.BaseMvpActivity;
import com.sanjiahantang.youjiarole.base.BaseView;
import com.sanjiahantang.youjiarole.config.SPConfig;
import com.sanjiahantang.youjiarole.module.me.scheme.beans.MeSchemeListBean;
import com.sanjiahantang.youjiarole.module.me.scheme.presenters.MeSchemePresenter;
import com.sanjiahantang.youjiarole.module.me.scheme.views.MeSchemeView;
import com.sanjiahantang.youjiarole.utils.SPUtils;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;

import java.util.List;

/**
 * Created by zy on 2017/9/8.
 */

public class MeSchemeListActivity extends BaseMvpActivity<MeSchemeView, MeSchemePresenter> implements MeSchemeView, View.OnClickListener, MeSchemeView.OnLoadListener {


    private View back;
    private MagicIndicator miTabs;
    private ListView listView;
    private String currentPhone;

    @Override
    public void showProgress() {
        super.showProgress($this);
    }

    @Override
    public MeSchemePresenter initPresenter() {
        return new MeSchemePresenter();
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_scheme_list);

        TextView title = (TextView) findViewById(R.id.title);
        title.setText("我的方案");
        back = findViewById(R.id.fl_Left);
        back.setOnClickListener(this);

        listView = (ListView) findViewById(R.id.listView);
        currentPhone = SPUtils.get($this, SPConfig.PHONE,"").toString();
    }

    @Override
    public void initData() {
        presenter.loadMySchemeListFirst($this);

        presenter.setOncliclisener(new BaseView.OnLoadListener() {
            @Override
            public void onLoad(Object content) {

                Object[] param = (Object[]) content;
                switch ((int) param[0]) {
                    case MESCHEMELIST_ONE:
                        QuickAdapter<MeSchemeListBean.Item> quickAdapter = new QuickAdapter<MeSchemeListBean.Item>($this, R.layout.item_project) {
                            @Override
                            protected void convert(BaseAdapterHelper helper, final MeSchemeListBean.Item item) {
                                helper.getView(R.id.ivRight).setVisibility(View.VISIBLE);
                                helper.setText(R.id.txtApartName,  item.getApartmentName());
                                helper.setText(R.id.txtName,  item.getUserName());
                                String city = item.getAddress();
                                if (item.getAddress() != null && item.getAddress().contains(" ")){
                                    String[] arr = item.getAddress().split(" ");
                                    if (arr.length >= 2){
                                        city = arr[1].replace("市","");
                                    }else if(arr.length > 0){
                                        city = arr[0];
                                    }else{
                                        city = item.getAddress();
                                    }
                                }else if(item.getAddress() != null){
                                    city = item.getAddress();
                                }
                                helper.setText(R.id.txtCity, city);
                                helper.getView().setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        Intent intent = new Intent(context,MeSchemeActivity.class);
                                        intent.putExtra(KEY_ID,""+item.getId());
                                        context.startActivity(intent);
                                    }
                                });

                                if (item.getIsSubcontract() == 1){
                                    helper.setVisible(R.id.txtSubcontract,true);
                                    if (currentPhone.equals(item.getDesignPhone()) && !TextUtils.isEmpty(item.getDesignRoleId())){
                                        helper.setText(R.id.txtSubcontract,"来自： " + item.getDesignRoleName() + "(" + item.getDesignRoleId() + ")");
                                    }else if(currentPhone.equals(item.getDesignRoleId()) && !TextUtils.isEmpty(item.getDesignPhone())){
                                        helper.setText(R.id.txtSubcontract,"分包： " + item.getDesignName() + "(" + item.getDesignPhone() + ")");
                                    }else {
                                        helper.setVisible(R.id.txtSubcontract,false);
                                    }
                                }
                            }
                        };
                        quickAdapter.addAll((List<MeSchemeListBean.Item>) param[1]);
                        listView.setAdapter(quickAdapter);
                        break;
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fl_Left:
                finish();
                break;

        }
    }

    @Override
    public void initTabs(CommonNavigatorAdapter adapter) {

    }

    @Override
    public void switchPages(int index) {

    }

    @Override
    public void initList(QuickAdapter adapter) {

    }
}
